<template>  
  <div class="system-sensors-edit">
    <!-- 添加或修改传感器设备对话框 -->
    <el-dialog v-model="isShowDialog" width="769px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-sensors-edit .el-dialog', '.system-sensors-edit .el-dialog__header']">修改角色</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="90px">        
        <el-form-item label="角色" prop="roleIds">
          <el-select
									v-model="formData.roleIds"
								placeholder="请选择角色"
									style="width: 100%" multiple
								>
									<el-option v-for="item of roleLists"  :key="item.id" :label="item.name" :value="item.id"> </el-option>
								</el-select>
        </el-form-item>   
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance } from 'vue';
import { ElMessage,} from 'element-plus';
import {
  updateSensors,  getSensors
} from "/@/api/system/sensors";
export default defineComponent({
  name:"index",
  props:{    
    statusOptions:{
      type:Array,
      default:()=>[]
    }, 
    roleLists:{
      type:Array,
      default:()=>[]
    },     
  },
  setup(props,{emit}) {    
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();    
    const state = reactive({
      loading:false,
      isShowDialog: false,
      formData: {        
        imeis: [],   
        roleIds:[]     
      },
      // 表单校验
      rules: {        
        roleIds : [
            { required: true, message: "角色不能为空", trigger: "blur" }
        ],        
      }
    });
    // 打开弹窗
    const openDialog = (imeis:any,id:any) => {
      resetForm();
      state.formData.imeis=imeis
      if(id){
        getSensors(id).then((res:any)=>{
          const data = res.data;          
          state.formData.roleIds=res.data.roleIds
      })
      }
      
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false; 
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          updateSensors(state.formData).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('sensorsList')
            }).finally(()=>{
              state.loading = false;
            })
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {        
        imeis: [],   
        roleIds:[]       
      }
    };    
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,      
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>  
</style>